package com.fhj.movie.fragment;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.fhj.movie.activity.FHCityListActivity;
import com.fhj.movie.activity.JyjCinemaDetailsActivity;
import com.fhj.movie.adapter.CinemaListAdapter;
import com.fhj.movie.baidumap.JyjMapActivity;
import com.fhj.movie.entity.JyjCinemaEntity;
import com.fhj.movie.entity.JyjDistrictEntity;
import com.fhj.movie.interfaces.CallbackInterface;
import com.fhj.movie.interfaces.MyApplication;
import com.fhj.movie_fhj_01.R;
import com.fhj.utils.HttpConnectionUtils;
import com.fhj.utils.JsonUtils;
import com.fhj.utils.ListOrder;
import com.fhj.utils.ViewAddHeadview;
import com.fhj.utils.ViewAddHeadview.addViewCallback;
import com.fhj.utils.ViewAddHeadview.spinnerCallback;

public class JyjMovieFragment extends Fragment implements OnItemClickListener {

	
	private ListView  listView;
	
	protected ImageView point;
	
	private int cityId=110000;
	
	private List<JyjDistrictEntity> districts; //获取到了城市区域详情
	
	private TextView city;

	private MyApplication application;
	
	private LinkedList<JyjCinemaEntity> cinemaList;//获取到的影院的原始信息
	
	private CinemaListAdapter adapter;//listView的适配器
	
	private ViewAddHeadview headview;//listview要添加的头部view类
	
	private FrameLayout  loadingfrFrameLayout;//加载界面
	private  ImageView  loadinganim;//加载界面动画
	
	private  double localLatitude;//经度
	private  double localLongitude;//纬度
	
	//更新adapter展示
	private Handler handler= new Handler(){
		

		public void handleMessage(android.os.Message msg) {
			if (msg.what==1) {
				
				loadingfrFrameLayout.setVisibility(View.GONE);
				
				ArrayList<JyjCinemaEntity> list = (ArrayList<JyjCinemaEntity>) ListOrder.list;
				
				adapter = new CinemaListAdapter();
				
				adapter.initData(getActivity(), list, districts,localLatitude,localLongitude);
				
				listView.setAdapter(adapter);
				
				headview.setSpinnerCallback(new spinnerCallback() {
					
					@Override
					public void spinnerCallBack(int id, int type) {
						if (id==R.id.movie_alltype) {
							List<JyjCinemaEntity> cinema = ListOrder.filterByOther(type, cinemaList);
							List<JyjCinemaEntity> orderDistrict = ListOrder.OrderDistrict(districts, cinema);
							adapter.initData(getActivity(), orderDistrict, districts, localLatitude, localLongitude);
							adapter.notifyDataSetChanged();
						}
						if (id==R.id.movie_orderby) {
							List<JyjCinemaEntity> cinema = ListOrder.filterByOther(type+4, cinemaList);
							List<JyjCinemaEntity> orderDistrict = ListOrder.OrderDistrict(districts, cinema);
							adapter.initData(getActivity(), orderDistrict, districts, localLatitude, localLongitude);
							adapter.notifyDataSetChanged();
						}
					}
				});
				
				headview.updateSpinner(cityId);
				
				headview.setCallback(new addViewCallback() {
					
					@Override
					public void addCallback(long id) {
						List<JyjCinemaEntity> cinema = ListOrder.filteringCinema(id, cinemaList);
						List<JyjCinemaEntity> orderDistrict = ListOrder.OrderDistrict(districts, cinema);
						adapter.initData(getActivity(), orderDistrict, districts,localLatitude,localLongitude);
						adapter.notifyDataSetChanged();
					}
				});
			}
		};
	};

	public void onCreate(Bundle savedInstanceState) {
		
		application = (MyApplication) getActivity().getApplication();
		
		localLatitude = application.getLatitude();
		localLongitude = application.getLongitude();
		
		super.onCreate(savedInstanceState);
	};
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View  view=inflater.inflate(R.layout.movie_main_fragment, container, false);
		
		Toast.makeText(getActivity(), ""+application.getAddress()+":"+localLatitude+":"+localLongitude, Toast.LENGTH_SHORT).show();
		
		loadingfrFrameLayout = (FrameLayout)view.findViewById(R.id.movie_details_framelayout);
		loadinganim = (ImageView)view.findViewById(R.id.movie_details_loading);
		Animation animation  = AnimationUtils.loadAnimation(getActivity(), R.anim.load_rotate);
		loadinganim.setAnimation(animation);
		
		city = (TextView)view.findViewById(R.id.btn_current_city);
		city.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent  intent = new Intent(getActivity(), FHCityListActivity.class);
				startActivityForResult(intent, 1100);
			}
		});
		
		listView=(ListView)view.findViewById(R.id.movie_listView);
		
		LinearLayout linearLayout= (LinearLayout)view.findViewById(R.id.of_film_head);
		linearLayout.setVisibility(View.GONE);
		
		TextView title = (TextView)view.findViewById(R.id.of_movie_head);
		title.setVisibility(View.VISIBLE);
		
		CheckBox settingOld = (CheckBox)view.findViewById(R.id.header_bar_gallery);
		settingOld.setVisibility(View.GONE);
		ImageButton setting= (ImageButton)view.findViewById(R.id.header_bar_location);
		setting.setVisibility(View.VISIBLE);
		setting.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 点击跳转到MapActivity界面
				Intent  intent = new Intent(getActivity(), JyjMapActivity.class);
				startActivity(intent);
			}
		});
		
		headview = new ViewAddHeadview(getActivity(), listView);
		headview.addSearchHeadView();//添加搜索
		headview.addADHeadView();//添加广告
		headview.addTypeHeadView(cityId);//添加分类
		
		/**
		 * 添加“全部影院”HeadView
		 */
		View headView3 = LayoutInflater.from(getActivity()).inflate(R.layout.movie_listview_headview_first, null);
		listView.addHeaderView(headView3);//listview添加头部
		
		listView.setOnItemClickListener(this);
		
		requestCityInfo(cityId);
		
		return view;
	}

	/**
	 * 接受城市返回的数据
	 */
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		Toast.makeText(getActivity(), ""+resultCode, Toast.LENGTH_SHORT).show();
		if (resultCode==android.app.Activity.RESULT_OK&&requestCode==1100) {
			String cityName = data.getStringExtra("cityName");
			
			city.setText(cityName);
			
			cityId = application.getCityCode();
			
			requestCityInfo(cityId);
		}
		
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	
	/**
	 * 根据城市id刷新城市数据
	 */
	private void requestCityInfo(final int cityId) {
		HttpConnectionUtils.getCinameData(getActivity(), cityId, new CallbackInterface() {
			
			@Override
			public void JsonCallback(JSONObject object) {
				cinemaList = JsonUtils.parseCinemas(object);
				HttpConnectionUtils.getCityDetailsData(getActivity(), cityId, new CallbackInterface() {
					
					@Override
					public void JsonCallback(JSONObject object) {
						districts = JsonUtils.parseDistrict(object);//解析城市信息
						ListOrder.OrderDistrict(districts, cinemaList);//进行城市区域的排序
						Message message = handler.obtainMessage(1);
						handler.sendMessage(message);
					}
				});
				
			}
		});
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		districts=null;
	}
	
	/**
	 * listview item点击事件
	 * 
	 *（非 Javadoc）
	 * 
	 * @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, android.view.View, int, long)
	 */
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		
		int headCount = listView.getHeaderViewsCount();
		
		Log.d("listview  item", "======"+(position-headCount));
		
		Intent intent = new Intent(getActivity(), JyjCinemaDetailsActivity.class);
		intent.putExtra("position", position-headCount);
		intent.putExtra("cityId", cityId);
		
		startActivity(intent);
	}
	@Override
	public void onDestroyView() {
		super.onDestroyView();
	}
}
